package com.coffee.admin.controller;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.common.service.SmsCodeService;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.core.util.CheckMobileUtil;
import com.coffee.core.util.HttpClientUtil;
import com.coffee.core.util.StringUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description 短信
 * @Author liaoyangfan
 * @Date 2021/3/9 16:33
 * @Version 1.0
 **/
@RestController
@RequestMapping()
public class SmsController {
    private static final Logger LOG = LoggerFactory.getLogger(SmsController.class);

    @DubboReference(check = false)
    private SmsCodeService smsService;

    @Value("${parameter.yunpian.url}")
    private String url;
    @Value("${parameter.yunpian.apikey}")
    private String apikey;

    /***
     * @Description: 短信发送
     * @param request
     * @param response
     * @return com.linggong.result.Result<java.lang.String>
     * @Author: liaoyangfan
     * @date: 2021/1/25 15:53
     */
    @RequestMapping("/sms/send")
    @OperLog(operModul = "短信模块", operDesc = "发送短信")
    public Result<String> sendVerificationCodeSms(@RequestBody JSONObject json, HttpServletRequest request, HttpServletResponse response) {

        String mobile = json.getString("mobile");
        String code = json.getString("code");
        String sendVerificationCodeSms = null;
        try {
            CheckMobileUtil.checkMobile(mobile);
            String smsTemplate = smsService.getSmsTemplate(code);
            String verificationCode = StringUtil.getRandomDigit(6);
            String replace = smsTemplate.replace("#code#", verificationCode);
            //短信发送
            Map<String, String> map = new HashMap<>(3);
            map.put("apikey", apikey);
            map.put("mobile", mobile);
            map.put("text", replace);

            LOG.info("=======短信发送(url:" + url + ")==============request=" + JSONObject.toJSONString(map));
            String ret = HttpClientUtil.doPostWithUtf8(url, map);
            LOG.info("=======短信发送ret:" + ret);
            JSONObject retJson = JSONObject.parseObject(ret);
            String resultCode = retJson.getString("code");
            if ("53".equals(resultCode)) {
                throw new BusinessException(53, "手机号接收超过频率限制");
            }
            if (!"0".equals(resultCode)) {
                throw BusinessException.build(400, "发送短信失败");
            }
            sendVerificationCodeSms = smsService.sendVerificationCodeSms(code, mobile, verificationCode);
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return Result.success(sendVerificationCodeSms, 200000, "短信发送成功");
    }


}
